/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Controller.VendaDAO;
import Model.Cliente;
import Model.Produto;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.dom4j.DocumentException;

/**
 *
 * @author iron
 */
@ManagedBean(name="relatorio")
public class RelatorioMBean {
    private Date inicio;
    private Date fim;
    
    
    public RelatorioMBean(){
        inicio = new Date();
        inicio.setDate(1);
        fim    = new Date();
    }
    
    
    public int totalProduto(Produto p){
        int total = 0;
        if(inicio == null || fim == null)
            return 0;
        try {
            total = VendaDAO.INSTANCE.vendasProduto(p, inicio, fim);
            
        } catch (Exception ex) {
            Logger.getLogger(RelatorioMBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(total);
        return total;
    }
    
    public int totalCompras(Cliente c){
        int total = 0;
        if(inicio == null || fim == null){
            return 0;
        }
        try{
          total = VendaDAO.INSTANCE.vendasCliente(c, inicio, fim);  
        }catch(Exception ex){
            ex.printStackTrace();
            return 0;
        }
        return total;
    }
    
    public void postProcessXLS(Object document) {  
        HSSFWorkbook wb = (HSSFWorkbook) document;  
        HSSFSheet sheet = wb.getSheetAt(0);  
        HSSFRow header = sheet.getRow(0);  

        HSSFCellStyle cellStyle = wb.createCellStyle();    
        cellStyle.setFillForegroundColor(HSSFColor.GREEN.index);  
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  

        for(int i=0; i < header.getPhysicalNumberOfCells();i++) {  
            HSSFCell cell = header.getCell(i);  

            cell.setCellStyle(cellStyle);  
        }  
    }  
  
    public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException, com.lowagie.text.DocumentException {  
        Document pdf = (Document) document;  
        pdf.open();  
        pdf.setPageSize(PageSize.A4);  

        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();  
        String logo = servletContext.getRealPath("") + File.separator + "images" + File.separator + "prime_logo.png";  

        pdf.add(Image.getInstance(logo));  
    }  
    
    public Date getInicio() {
        return inicio;
    }

    public void setInicio(Date inicio) {
        this.inicio = inicio;
    }

    public Date getFim() {
        return fim;
    }

    public void setFim(Date fim) {
        this.fim = fim;
    }
    
    
    
    
}
